import numpy as np
from sklearn.linear_model import SGDRegressor
import matplotlib.pyplot as plt

# 训练数据
x = np.array([[100], [113], [90], [89], [60], [70], [50], [45], [55], [78]])
y = np.array([[301], [324], [285], [296], [200], [260], [300], [120], [180], [245]])

# 创建并训练SGD回归模型，使用huber损失函数
model = SGDRegressor(loss='huber', max_iter=5000, random_state=42)
model.fit(x, y.ravel()) 

# 对训练数据进行预测
y_pred = model.predict(x)

# 测试数据
x_test = np.array([[103], [115], [90], [89], [60], [70], [50], [45], [55], [78]])
y_test = np.array([[301], [344], [275], [276], [206], [210], [160], [124], [190], [235]])

# 计算评估指标
mse = np.average((y_pred - y.ravel())**2) 
rmse = np.sqrt(mse) 
r2 = model.score(x_test, y_test.ravel())

# 输出评估结果
print(f"均方误差(MSE)为：{mse:.4f}") 
print(f"均方根误差(RMSE)为：{rmse:.4f}")
print(f"决定系数(R²)为：{r2:.4f}")